package com.yuntongxun.ecdemo.voicechat.ui.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.listeners.CheckBoxCheckListener;
import com.yuntongxun.ecdemo.voicechat.listeners.IRecyclerViewItemListener;
import com.yuntongxun.ecdemo.voicechat.view.MyImageSpan;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by zhuonan on 2016/11/25.
 */

public class ChoosePartnersAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<UserInfo> notes;
    private CheckBoxCheckListener mListener;
    MyImageSpan flagSpan;
    MyImageSpan genderSpan;
    List<Integer> checkPosition;
    public ChoosePartnersAdapter(Context context, List<UserInfo> noteList, CheckBoxCheckListener myItemClickListener) {
        mContext = context;
        notes = noteList;
        mListener = myItemClickListener;
        checkPosition = new ArrayList<Integer>(notes.size());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = View.inflate(parent.getContext(), R.layout.adapter_choose_partners_item, null);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder=(ViewHolder)holder;

        String name = notes.get(position).getNickName()+"  ";
        String age = "  "+notes.get(position).getAge()+"  ";
        String nativel = "  "+notes.get(position).getNativeLanguage()+"  ";
        String learn = "  "+notes.get(position).getLearningLanguage();

        if (notes.get(position).getGender()!=null&&notes.get(position).getGender().equals("男")){
            genderSpan = new MyImageSpan(mContext, R.drawable.ic_man);
        }else{
            genderSpan = new MyImageSpan(mContext, R.drawable.ic_woman);
        }

        MyImageSpan nativeSpan = new MyImageSpan(mContext, R.drawable.ic_speak);
        MyImageSpan learnSpan = new MyImageSpan(mContext, R.drawable.ic_learn);

        try {
            InputStream is = mContext.getAssets().open("flag/"+notes.get(position).getCountry()+".png");
            flagSpan = new MyImageSpan(mContext, BitmapFactory.decodeStream(is));
            is.close();
        } catch (IOException e) {
            Log.e("----",e.toString());
        }

        viewHolder.textView.setText("");
        //姓名
        SpannableString nameShow = new SpannableString(name);
        ForegroundColorSpan blackSpan = new ForegroundColorSpan(Color.BLACK);
        nameShow.setSpan(blackSpan,0,name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.textView.append(nameShow);

        //插入性别图标
        SpannableString showString = new SpannableString("1");
        showString.setSpan(genderSpan,0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.textView.append(showString);

        //年龄颜色
        SpannableString ageShow = new SpannableString(age);
        ForegroundColorSpan yellowSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.voice_yellow));
        ageShow.setSpan(yellowSpan,0,age.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.textView.append(ageShow);

        //插入国旗图标
        showString.setSpan(flagSpan, 0, 1 , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.textView.append(showString);

        viewHolder.textView.append("  ");

        //插入本地语言的图标
        showString.setSpan(nativeSpan, 0, 1 , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.textView.append(showString);

        //本地语言颜色
        SpannableString nativeShow = new SpannableString(nativel);
        ForegroundColorSpan greenSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.gray3));
        nativeShow.setSpan(greenSpan,0,nativel.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.textView.append(nativeShow);

        //插入学习的图标
        showString.setSpan(learnSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.textView.append(showString);

        //学习语言颜色
        SpannableString learnShow = new SpannableString(learn);
        ForegroundColorSpan greenSpan1 = new ForegroundColorSpan(mContext.getResources().getColor(R.color.gray3));
        learnShow.setSpan(greenSpan1,0,learn.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.textView.append(learnShow);

        viewHolder.cb.setTag(new Integer(position));//设置tag 否则划回来时选中消失

        if (checkPosition != null){
            viewHolder.cb.setChecked((checkPosition.contains(new Integer(position)) ? true : false));
        }else{
            viewHolder.cb.setChecked(false);
        }


        viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mListener!=null){

                    if (isChecked){
                        if (!checkPosition.contains(viewHolder.cb.getTag())){
                            checkPosition.add((Integer)viewHolder.cb.getTag());
                            mListener.onCheckBoxCheck(position,isChecked);
                        }
                    }else{
                        if (checkPosition.contains(viewHolder.cb.getTag())){
                            checkPosition.remove(viewHolder.cb.getTag());
                            mListener.onCheckBoxCheck(position,isChecked);
                        }
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (notes!=null){
            return notes.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{


        @InjectView(R.id.conversation_info)
        TextView textView;

        @InjectView(R.id.cb)
        CheckBox cb;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

//        @OnClick(R.id.card_view_layout)
//        public void clickItem() {
//            if (mListener != null) {
//                mListener.onItemClick(getPosition());
//            }
//        }
    }
}
